package pl.enigmatic.util;

import java.awt.Font;
import java.awt.Image;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URISyntaxException;
import java.net.URL;

import javax.swing.ImageIcon;


public abstract class ResourceLocator {

	protected static final String NL = "\n";
	protected static final String SEP = "/";

	protected final Class<?> resolver;
	protected final Path locationBase;

	protected ResourceLocator(final Class<?> resolver, final String base) {
		this.resolver = resolver;
		locationBase = new Path(base);
	}

	public ResourceLocator(final Class<?> resolver) {
		this(resolver, "");
	}

	protected ResourceLocator() {
		this(ResourceLocator.class);
	}

	protected final String fullPath(final String path) {
		return locationBase.getAbsolutePath(path);
	}

	public final URL getURL(final String path) {
		return resolver.getResource(fullPath(path));
	}

	public final boolean resExists(final String path) {
		return getURL(path) != null;
	}

	public final InputStream getStream(final String path) {
		return resolver.getResourceAsStream(fullPath(path));
	}

	public InputStream getStream(final URL url) {
		return null == url ? null : resolver.getResourceAsStream(url.getPath());
	}

	public static File getFile(final URL url) {
		try {
			return new File(url.toURI());
		} catch (final URISyntaxException e) {
			return null;
		}
	}

	public final File getFile(final String path) {
		return getFile(getURL(fullPath(path)));
	}

	public InputStream getImageStream(final String path) {
		return getStream(path);
	}

	public ImageIcon getImageIcon(final String path) {
		final URL url = getURL(path);
		final ImageIcon img = new ImageIcon(url);
		return img;
	}

	public final Image getImage(final String path) {
		return getImageIcon(path).getImage();
	}

	public Font getFont(final String path) {
		Font font = null;
		try {
			final InputStream is;
			is = getStream(path);
			font = Font.createFont(Font.TRUETYPE_FONT, is);
		} catch (final Exception ex) {}
		return font;
	}

	public final Font getFont(final String name, final String type) {
		return getFont(name + SEP + type + ".ttf");
	}

	public final static String readText(final InputStream is) {
		try {
			final BufferedReader in = new BufferedReader(new InputStreamReader(is));
			final StringBuffer str = new StringBuffer();
			final char[] buffer = new char[1024];
			int r = 0;
			while ((r = in.read(buffer)) > 0) {
				str.append(buffer, 0, r);
			}
			in.close();
			return str.toString();
		} catch (final IOException e) {
			return null;
		}
	}

	public static String readText(final File file) {
		String res = "";
		try {
			final BufferedReader in = new BufferedReader(new FileReader(file));
			final StringBuffer str = new StringBuffer();
			String line;
			while ((line = in.readLine()) != null) {
				str.append(line + NL);
			}
			in.close();
			res = str.toString();
		} catch (final Exception e) {}
		return res.substring(0, res.length() - 1);
	}

	public final String readText(final String filepath) {
		return readText(getStream(filepath));
	}

	public static void saveText(final String text, final String path) throws IOException {
		final Writer out = new OutputStreamWriter(new FileOutputStream(path));
		try {
			out.write(text);
		} finally {
			out.close();
		}
	}

	public final String[] readLines(final String filename) {
		return readText(filename).split(NL);
	}

}
